using UnityEngine;

[AddComponentMenu("BoneCracker Games/Realistic Car Controller/Camera/Top Camera")]
public class RCC_TopCamera : MonoBehaviour
{
	public Transform playerCar;

	private Rigidbody playerRigid;

	private Camera cam;

	public GameObject pivot;

	public float distance = 20f;

	private float distanceOffset;

	public float maximumDistanceOffset = 10f;

	private float targetFieldOfView = 60f;

	public float minimumOrtSize = 7.5f;

	public float maximumOrtSize = 12.5f;

	private Vector3 targetPosition;

	private Vector3 pastFollowerPosition = Vector3.zero;

	private Vector3 pastTargetPosition = Vector3.zero;

	private float speed;

	public Transform _playerCar
	{
		get
		{
			return playerCar;
		}
		set
		{
			playerCar = value;
			GetPlayerCar();
		}
	}

	private void Awake()
	{
		cam = GetComponentInChildren<Camera>();
	}

	private void GetPlayerCar()
	{
		if ((bool)playerCar)
		{
			playerRigid = playerCar.GetComponent<Rigidbody>();
			base.transform.position = playerCar.transform.position;
			if ((bool)playerCar.GetComponent<RCC_CameraConfig>())
			{
				playerCar.GetComponent<RCC_CameraConfig>().SetCameraSettings();
			}
		}
	}

	public void SetPlayerCar(GameObject player)
	{
		_playerCar = player.transform;
	}

	private void Update()
	{
		if (!playerCar || !playerRigid)
		{
			GetPlayerCar();
			return;
		}
		speed = Mathf.Lerp(speed, playerRigid.velocity.magnitude * 3.6f, Time.deltaTime * 0.5f);
		distanceOffset = Mathf.Lerp(0f, maximumDistanceOffset, speed / 100f);
		targetFieldOfView = Mathf.Lerp(minimumOrtSize, maximumOrtSize, speed / 100f);
		cam.orthographicSize = targetFieldOfView;
		targetPosition = playerCar.position;
		targetPosition += playerCar.rotation * Vector3.forward * distanceOffset;
		base.transform.position = SmoothApproach(pastFollowerPosition, pastTargetPosition, targetPosition, Mathf.Clamp(0.5f, speed, float.PositiveInfinity));
		pastFollowerPosition = base.transform.position;
		pastTargetPosition = targetPosition;
		pivot.transform.localPosition = new Vector3(0f, 0f, 0f - distance);
	}

	private Vector3 SmoothApproach(Vector3 pastPosition, Vector3 pastTargetPosition, Vector3 targetPosition, float delta)
	{
		if (float.IsNaN(delta) || float.IsInfinity(delta) || pastPosition == Vector3.zero || pastTargetPosition == Vector3.zero || targetPosition == Vector3.zero)
		{
			return base.transform.position;
		}
		float num = Time.deltaTime * delta;
		Vector3 vector = (targetPosition - pastTargetPosition) / num;
		Vector3 vector2 = pastPosition - pastTargetPosition + vector;
		return targetPosition - vector + vector2 * Mathf.Exp(0f - num);
	}
}
